package xhwl.insurance.ui;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import xhwl.android.common.util.SPUtil;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.adapter.PerformanceAdapter;
import xhwl.insurance.http.api.PerformanceApi;
import xhwl.insurance.http.bean.resp.HzOrderResp;
import xhwl.insurance.util.XHLog;

import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

import static xhwl.insurance.R.id.pullToRefreshListView;

/**
 * HMO 售卡统计
 * chengang on 2017/7/28.
 */

public class PerformanceActivity extends BaseActivity implements View.OnClickListener {

    private TextView card_selling_num;
    private ListView mListView;
    private PerformanceAdapter adapter;
    private ArrayList<HzOrderResp> mlistVo = new ArrayList<HzOrderResp>();
    private LinearLayout emptyView, ll_all;
    private TextView tv_type;
    private TextView tv_date;
    private ImageView iv_direction_right, iv_direction_right2, iv_direction_ablove, iv_direction_ablove2;
    private String types = "";
    private String dates = "";
    private RelativeLayout rl_date, rl_type;
    private SimpleAdapter mMenuAdapter1;
    private SimpleAdapter mMenuAdapter2;
    private int supplierMenuIndex = 0;

    private List<Map<String, String>> mMenuData1 = new ArrayList<>();
    private List<Map<String, String>> mMenuData2 = new ArrayList<>();
    private static final String MENU_DATA_KEY = "name";
    private static final String SALE_TYPE = "";
    private ListView mPopListView;
    private PopupWindow mPopupWindow;
    private RelativeLayout mSelectedAll;

    private ImageView mSelected;
    private ImageView mUnchecked;
    private View view_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.performance);
        initPopMenu();
        initData3();
    }


    @Override
    public void initViews() {
        initTitleViews();
        card_selling_num = (TextView) findViewById(R.id.card_selling_num);
        emptyView = (LinearLayout) findViewById(R.id.emptyView);
        ll_all = (LinearLayout) findViewById(R.id.ll_all);

        mListView = (ListView) findViewById(pullToRefreshListView);
        tv_type = (TextView) findViewById(R.id.tv_type);
        tv_date = (TextView) findViewById(R.id.tv_date);
        iv_direction_right = (ImageView) findViewById(R.id.iv_direction_right);
        iv_direction_ablove = (ImageView) findViewById(R.id.iv_direction_ablove);

        iv_direction_ablove2 = (ImageView) findViewById(R.id.iv_direction_ablove2);
        iv_direction_right2 = (ImageView) findViewById(R.id.iv_direction_right2);
        rl_date = (RelativeLayout) findViewById(R.id.rl_date);
        rl_type = (RelativeLayout) findViewById(R.id.rl_type);

        adapter = new PerformanceAdapter(PerformanceActivity.this, mlistVo);
        mListView.setAdapter(adapter);

    }

    @Override
    protected void setUpTitleView() {
        titleTextView.setText("HMO售卡统计");
        super.setUpTitleView();
    }

    @Override
    public void initListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                HzOrderResp orderResp = mlistVo.get(position);
                startActivity(new Intent(PerformanceActivity.this, CardDetailsActivity.class)
                        .putExtra("sale_type", orderResp.getSALE_TYPE())
                        .putExtra("days", dates)
                );
            }
        });
        rl_date.setOnClickListener(this);
        rl_type.setOnClickListener(this);
    }

    @Override
    public void loadData() {
        requestList("", "");
    }

    private void requestList(String types, String dates) {
        mlistVo.clear();
        String link_phone = SPUtil.getPersonInfoSp().getString(SPUtil.SPPersonInfo.LINK_MOBILE, "");
        PerformanceApi api = new PerformanceApi(link_phone, types, dates);
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<ArrayList<HzOrderResp>>() {
            @Override
            public void onNext(ArrayList<HzOrderResp> resp) {
                try {
                    if (null != resp && 0 < resp.size()) {
                        int sum = 0;
                        for (int i = 0; i < resp.size(); i++) {
                            sum += Integer.valueOf(resp.get(i).getNUM());
                        }
                        card_selling_num.setText("您共计售卡" + sum + "张");

                        mlistVo.addAll(resp);
                        initData2();
                        ll_all.setVisibility(View.VISIBLE);
                        emptyView.setVisibility(View.GONE);
                    } else {
                        ll_all.setVisibility(View.GONE);
                        emptyView.setVisibility(View.VISIBLE);
                    }

                    adapter.notifyDataSetChanged();
                } catch (Exception e) {
                    XHLog.e(e.getMessage());
                    ll_all.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
                mListView.setVisibility(View.GONE);
                emptyView.setVisibility(View.VISIBLE);
            }
        });
    }

    private void initData3() {
        String[] sorts = getResources().getStringArray(R.array.supplier_product);
        for (int i = 0; i < sorts.length; i++) {
            Map<String, String> map = new HashMap<>();
            map.put(MENU_DATA_KEY, sorts[i]);
            mMenuData2.add(map);
        }
    }

    private void initData2() {
        for (int i = 0; i < mlistVo.size(); i++) {
            Map<String, String> map = new HashMap<>();
            map.put(MENU_DATA_KEY, mlistVo.get(i).getSALE_NAME());
            map.put(SALE_TYPE, mlistVo.get(i).getSALE_TYPE());
            mMenuData1.add(map);
        }

    }

    private void initPopMenu() {
        View popView = LayoutInflater.from(PerformanceActivity.this).inflate(R.layout.layout_popwin_supplier_list, null);
        mPopListView = (ListView) popView.findViewById(R.id.popwin_list_view);
        mPopupWindow = new PopupWindow(popView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setOutsideTouchable(false);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setFocusable(true);
        mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                iv_direction_right.setVisibility(View.GONE);
                iv_direction_ablove.setVisibility(View.VISIBLE);
                mListView.setVisibility(View.VISIBLE);
                iv_direction_right2.setVisibility(View.GONE);
                iv_direction_ablove2.setVisibility(View.VISIBLE);
                ll_all.setBackgroundResource(R.color.white);

            }
        });

        view_1 = (View) popView.findViewById(R.id.view_1);
        popView.findViewById(R.id.popwin_bottom).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindow.dismiss();
            }
        });
        mSelected = (ImageView) popView.findViewById(R.id.iv_selected_all);
        mUnchecked = (ImageView) popView.findViewById(R.id.iv_unchecked_all);

        mSelectedAll = (RelativeLayout) popView.findViewById(R.id.rl_selected_all);

        mSelectedAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mSelected.setVisibility(View.VISIBLE);
                mUnchecked.setVisibility(View.GONE);

                iv_direction_right.setVisibility(View.GONE);
                iv_direction_ablove.setVisibility(View.VISIBLE);

                tv_type.setText("全部");
                types = "";
                requestList2(types, dates);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPopupWindow.dismiss();
                    }
                }, 300);

            }
        });


        mMenuAdapter1 = new SimpleAdapter(PerformanceActivity.this, mMenuData1, R.layout.item_popwin_list,
                new String[]{"name"}, new int[]{R.id.item_popwin_tv});

        mMenuAdapter2 = new SimpleAdapter(PerformanceActivity.this, mMenuData2, R.layout.item_popwin_list,
                new String[]{"name"}, new int[]{R.id.item_popwin_tv,});
        mPopListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (supplierMenuIndex) {
                    case 0:
                        ImageView iv_selected_all = (ImageView) view.findViewById(R.id.iv_selected_all);
                        ImageView iv_unchecked_all = (ImageView) view.findViewById(R.id.iv_unchecked_all);
                        iv_selected_all.setVisibility(View.VISIBLE);
                        iv_unchecked_all.setVisibility(View.GONE);
                        mSelected.setVisibility(View.GONE);
                        mUnchecked.setVisibility(View.VISIBLE);

                        iv_direction_right.setVisibility(View.GONE);
                        iv_direction_ablove.setVisibility(View.VISIBLE);

                        String type = mMenuData1.get(i).get(MENU_DATA_KEY);
                        if (type.length() > 8) {
//                            tv_type.setText(type.substring(0, 4) + "**" + type.substring(type.length() - 8));
                            tv_type.setText( ".." +type.substring(type.length() - 9));
                        } else {
                            tv_type.setText(mMenuData1.get(i).get(MENU_DATA_KEY));
                        }
                        types = mMenuData1.get(i).get(SALE_TYPE);
                        requestList2(types, dates);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mPopupWindow.dismiss();
                            }
                        }, 300);
                        break;
                    case 1:
                        ImageView iv_selected_all2 = (ImageView) view.findViewById(R.id.iv_selected_all);
                        ImageView iv_unchecked_all2 = (ImageView) view.findViewById(R.id.iv_unchecked_all);
                        iv_selected_all2.setVisibility(View.VISIBLE);
                        iv_unchecked_all2.setVisibility(View.GONE);

                        iv_direction_right2.setVisibility(View.GONE);
                        iv_direction_ablove2.setVisibility(View.VISIBLE);

                        tv_date.setText(mMenuData2.get(i).get(MENU_DATA_KEY));

                        if (mMenuData2.get(i).get(MENU_DATA_KEY).equals("全部")) {
                            dates = "";
                        } else if (mMenuData2.get(i).get(MENU_DATA_KEY).equals("今日")) {
                            dates = "1";
                        } else if (mMenuData2.get(i).get(MENU_DATA_KEY).equals("近三天")) {
                            dates = "3";
                        } else if (mMenuData2.get(i).get(MENU_DATA_KEY).equals("近一周")) {
                            dates = "7";
                        } else if (mMenuData2.get(i).get(MENU_DATA_KEY).equals("近一个月")) {
                            dates = "30";
                        } else if (mMenuData2.get(i).get(MENU_DATA_KEY).equals("近半年")) {
                            dates = "182";
                        } else if (mMenuData2.get(i).get(MENU_DATA_KEY).equals("近一年")) {
                            dates = "365";
                        } else {
                            dates = "";
                        }
                        requestList2(types, dates);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mPopupWindow.dismiss();
                            }
                        }, 300);

                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_type:
                mSelectedAll.setVisibility(View.VISIBLE);
                ll_all.setBackgroundResource(R.color.bg_gray);
                mListView.setVisibility(View.GONE);
                view_1.setVisibility(View.VISIBLE);
                iv_direction_right.setVisibility(View.VISIBLE);
                iv_direction_ablove.setVisibility(View.GONE);
                tv_type.setTextColor(getResources().getColor(R.color.black));
                mPopListView.setAdapter(mMenuAdapter1);
                mPopupWindow.showAsDropDown(rl_type, 0, 2);
                supplierMenuIndex = 0;
                break;
            case R.id.rl_date:
                mSelectedAll.setVisibility(View.GONE);
                ll_all.setBackgroundResource(R.color.bg_gray);
                mListView.setVisibility(View.GONE);
                view_1.setVisibility(View.VISIBLE);
                iv_direction_right2.setVisibility(View.VISIBLE);
                iv_direction_ablove2.setVisibility(View.GONE);
                tv_date.setTextColor(getResources().getColor(R.color.black));
                mPopListView.setAdapter(mMenuAdapter2);
                mPopupWindow.showAsDropDown(rl_date, 0, 2);
                supplierMenuIndex = 1;
                break;
        }
    }


    private void requestList2(String types, String dates) {
        mlistVo.clear();
        String link_phone = SPUtil.getPersonInfoSp().getString(SPUtil.SPPersonInfo.LINK_MOBILE, "");
        PerformanceApi api = new PerformanceApi(link_phone, types, dates);
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<ArrayList<HzOrderResp>>() {
            @Override
            public void onNext(ArrayList<HzOrderResp> resp) {
                try {
                    if (null != resp && 0 < resp.size()) {
                        mlistVo.addAll(resp);
                        ll_all.setVisibility(View.VISIBLE);
                        emptyView.setVisibility(View.GONE);
                    } else {
                        ll_all.setVisibility(View.GONE);
                        emptyView.setVisibility(View.VISIBLE);
                    }

                    adapter.notifyDataSetChanged();
                } catch (Exception e) {
                    XHLog.e(e.getMessage());
                    ll_all.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
                mListView.setVisibility(View.GONE);
                emptyView.setVisibility(View.VISIBLE);
            }
        });


    }

}
